/*********************************************************
          File Name:allocator.cpp
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Thu 17 Sep 2015 07:30:25 PM CST
**********************************************************/

#include <iostream>
#include <string>
#include <memory>       // allocator

using std::string;
using std::allocator;   // raw memory (such as (void*)malloc(size) in C)

int main()
{
        allocator<string> alloc;        // create an allocator Object

        auto const p = alloc.allocate(3); // raw memory of 3 string objects
/*
        for(int i = 0; i < 3; i++)
                p[i] = "this is test";

        for(int i = 0; i < 3; i++)
                std::cout << p[i] << std::endl;
        // raw memory is OK, but cause memory leak
*/
        
        for(int i = 0; i < 3; i++)
                alloc.construct(&p[i], "OK");

        for(int i = 0; i < 3; i++)
                std::cout << p[i] << std::endl;

        for(int i = 0; i < 3; i++)
                alloc.destroy(&p[i]);

        alloc.deallocate(p, 3);

        return 0;
}
